<template>
    <div class="app-container">
        <section class="spilt_cell">
            <div>下单时间：{{orderInfo.createTime}}</div>
            <div>出厂批次：{{orderInfo.batch}}</div>
            <div>所属医院：{{orderInfo.hospitalName}}</div>
            <div>进厂批次：{{orderInfo.enterBatchCode}}</div>
            <div>状态：{{orderInfo.statusName}}</div>
            <div>打捆数量：{{orderInfo.orderCount}}</div>
            <div>打包数量：{{orderInfo.total_bagCnt}}</div>
            <div>衣物总量：{{orderInfo.clothesCnt}}</div>
            <div>缝补总量：{{orderInfo.mendCnt}}</div>
        </section>
        <div style="height: 42px;line-height: 32px;">
            <el-button type="primary" icon="document" @click="handleDownload" :loading="downloadLoading" style="float: right;">下载报表</el-button>
        </div>
        <el-table :key='tableKey' :data="orderList" v-loading="listLoading" element-loading-text="给我一点时间" border fit highlight-current-row style="width: 100%" stripe>
            <el-table-column align="center" type="index" label="序号" width="60px"></el-table-column>
            <el-table-column align="center" label="打捆编号" prop="balingCode"></el-table-column>
            <el-table-column align="center" label="打捆时间" prop="createTime" width="160px"></el-table-column>
            <el-table-column align="center" label="品类" prop="typeName"></el-table-column>
            <el-table-column align="center" label="科室">
                <template slot-scope="scope">
                    {{scope.row.departmentName || '--'}}
                </template>
            </el-table-column>
            <el-table-column align="center" label="分类" prop="categoryName"></el-table-column>
            <el-table-column align="center" label="衣物数量" prop="clothesCnt"></el-table-column>
            <el-table-column align="center" label="缝补数量" prop="mendCnt"></el-table-column>
            <el-table-column align="center" label="热塑码">
                <template slot-scope="scope">
                    {{scope.row.clothesBarCode || '--'}}
                </template>
            </el-table-column>
            <el-table-column align="center" label="打捆录入" prop="createUserName"></el-table-column>
        </el-table>
        <div class="pagination-container">
            <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="listQuery.page" :page-sizes="[20,50,100]" :page-size="listQuery.size" layout="total, sizes, prev, pager, next" :total="total">
            </el-pagination>
        </div>
    </div>
</template>

<script>
  import { parseTime } from '@/utils'
  import { factoryBatchOrderByCode, fetchFactoryBatchOrderDetail } from '@/api/factory'
  import exportExcel from '@/components/Excel/exportExcel'

  export default {
    name: 'OrderDetail',
    data() {
      return {
        tableKey: 0,
        orderList: [],
        total: null,
        listLoading: false,
        downloadLoading: false,
        listQuery: {
          page: 1,
          size: 20
        },
        orderId: '',
        orderInfo: {}
      }
    },
    components: {
      exportExcel
    },
    created: function() {
      if (this.$route.query.id) {
        this.orderId = this.$route.query.id
        this.getInfo()
        this.getDetail()
        return
      }
      this.$message({ message: '参数错误，请返回重试', type: 'error', showClose: true })
    },
    watch: {
      '$route.query.id'(val) {
        if (val) {
          this.orderId = val
          this.getInfo()
          this.getDetail()
        }
      }
    },
    filters: {
      parseTime
    },
    methods: {
      getDetail() {
        this.listLoading = true
        fetchFactoryBatchOrderDetail({ page: this.listQuery.page, size: this.listQuery.size, batchCode: this.orderId }).then(response => {
          this.listLoading = false
          this.orderList = response.data && response.data.list || []
          this.total = Number.parseInt(response.data.count)
        }).catch(e => {
          this.listLoading = false
        })
      },
      getInfo() {
        factoryBatchOrderByCode({ code: this.orderId }).then(response => {
          this.orderInfo = response.data || {}
        })
      },
      handleSizeChange(val) {
        this.listQuery.size = val
        this.getDetail()
      },
      handleCurrentChange(val) {
        this.listQuery.page = val
        this.getDetail()
      },
      handleDownload() {
        this.downloadLoading = true
        fetchFactoryBatchOrderDetail({ page: 1, size: 1000, batchCode: this.orderId }).then(response => {
          var excelList = response.data && response.data.list || []
          if (!excelList || !excelList.length) {
            this.$message({ message: '没有可下载的报表', type: 'error', showClose: true })
            this.downloadLoading = false
            return
          }
          import('@/vendor/Export2Excel').then(excel => {
            const tHeader = ['打捆编号', '打捆时间', '品类', '科室', '分类', '衣物数量', '缝补数量', '热塑码', '打捆录入']
            const filterVal = ['balingCode', 'createTime', 'typeName', 'departmentName', 'categoryName', 'clothesCnt', 'mendCnt', 'clothesBarCode', 'createUserName']
            const data = this.formatJson(filterVal, excelList)
            excel.export_json_to_excel({
              header: tHeader,
              data,
              filename: '出厂批次详情' + this.orderId,
              autoWidth: true
            })
            this.downloadLoading = false
          })
        }).catch(e => {
          this.downloadLoading = false
        })
      },
      formatJson(filterVal, jsonData) {
        return jsonData.map(v => filterVal.map(j => {
          if (j === 'createTime') {
            return parseTime(v[j])
          } else {
            return v[j]
          }
        }))
      }
    }
  }
</script>

<style scoped>
    .spilt_cell {
        margin-bottom: 20px;
        height: 120px;
        padding: 0px 20px;
    }
    .spilt_cell div {
        display: inline-block;
        width: 50%;
        float: left;
        height: 35px;
        line-height: 35px;
    }
    .pd1020 {
        padding: 10px 20px;
    }
</style>
